package cn.edu.scau.dormitory_management_system.controller;

import cn.edu.scau.dormitory_management_system.entity.RegisterEntity;
import cn.edu.scau.dormitory_management_system.enums.RegisterTypeEnum;
import cn.edu.scau.dormitory_management_system.service.RegisterService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("admin/register")
public class RegisterController {
//    日志
    private static final Logger LOGGER = LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    RegisterService registerService;

    @PostMapping("create")
    public Object createRegister(HttpServletRequest request, @RequestBody RegisterEntity registerEntity) {

        //todo: 如有传入stuId此处应比对，但我未找到与查找学生相关service

        JSONObject object = new JSONObject();

//        判断类型
//        RegisterTypeEnum registerTypeEnum = null;
//        try {
//            registerTypeEnum = RegisterTypeEnum.valueOf(registerEntity.getType().toUpperCase());
//        } catch (IllegalArgumentException e) {
//            object.put("code", 0);
//            object.put("msg", "登记类型错误");
//            return object;
//        }
//
//        registerEntity.setType(registerTypeEnum.getName());
        boolean flag = registerService.insertRegister(registerEntity);
        if (flag) {
            object.put("code", 200);
            object.put("msg", "操作成功");
        } else {
            object.put("code", 0);
            object.put("msg", "操作失败");
        };

        return object;
    }

    @PostMapping("delete")
    public Object deleteRegister(HttpServletRequest request, @RequestBody RegisterEntity registerEntity) {

        JSONObject object = new JSONObject();

        //        判断类型
//        RegisterTypeEnum registerTypeEnum = null;
//        try {
//            registerTypeEnum = RegisterTypeEnum.valueOf(registerEntity.getType().toUpperCase());
//        } catch (IllegalArgumentException e) {
//            object.put("code", 500);
//            object.put("msg", "登记类型错误");
//            return object;
//        }

//        registerEntity.setType(registerTypeEnum.getName());
        boolean flag = registerService.deleteRegister(registerEntity);

        if (flag) {
            object.put("code", 200);
            object.put("msg", "操作成功");
        } else {
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("update")
    public Object updateRegister(HttpServletRequest request, @RequestBody RegisterEntity registerEntity) {

        JSONObject object = new JSONObject();

        //        判断类型
//        RegisterTypeEnum registerTypeEnum = null;
//        try {
//            registerTypeEnum = RegisterTypeEnum.valueOf(registerEntity.getType().toUpperCase());
//        } catch (IllegalArgumentException e) {
//            object.put("code", 500);
//            object.put("msg", "登记类型错误");
//            return object;
//        }

//        registerEntity.setType(registerTypeEnum.getName());
        boolean flag = registerService.updateRegister(registerEntity);

        if (flag) {
            object.put("code", 200);
            object.put("msg", "操作成功");
        } else {
            object.put("code", 0);
            object.put("msg", "操作失败");
        }

        return object;
    }

    @PostMapping("findRegisterByTel")
    public Object findRegisterByTel(
            HttpServletRequest request,
            @RequestParam(value = "tel", defaultValue = "") String tel,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "8") int size
    ) {

        JSONObject object = new JSONObject();
        IPage<RegisterEntity> page = new Page<>(current, size);
        QueryWrapper<RegisterEntity> wrapper = new QueryWrapper<>();

        wrapper.like("tel", tel);
        page = registerService.page(page, wrapper);

        if (page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        } else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("findRegisterByType")
    public Object findRegisterByType(
            HttpServletRequest request,
            String type,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "8") int size
    ) {
//        精确查找

        JSONObject object = new JSONObject();

//        RegisterTypeEnum registerTypeEnum = null;
//        try {
//            registerTypeEnum = RegisterTypeEnum.valueOf(type.toUpperCase());
//        } catch (IllegalArgumentException e) {
//            object.put("code", 500);
//            object.put("msg", "登记类型错误");
//            return object;
//        }

        IPage<RegisterEntity> page = new Page<>(current, size);
        QueryWrapper<RegisterEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);

        page = registerService.page(page, wrapper);
//        List<RegisterEntity> registerEntityList = registerService.findRegisterByType(type);

        if (page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        } else {
            object.put("code", 200);
            object.put("page", page);
        }

        return object;
    }

    @PostMapping("findRegisterByReason")
    public Object findRegisterByReason(
            HttpServletRequest request,
            @RequestParam("reason") String reason,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "8") int size
    ) {
//        模糊查找

        JSONObject object = new JSONObject();
        IPage<RegisterEntity> page = new Page<>(current, size);
        QueryWrapper<RegisterEntity> wrapper = new QueryWrapper<>();
        wrapper.like("reason", reason);

        page = registerService.page(page, wrapper);
//        List<RegisterEntity> registerEntityList = registerService.findRegisterByReason(reason);

        if (page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        } else {
            object.put("code", 200);
            object.put("msg", page);
        }

        return object;
    }

    @PostMapping("findRegisterBetweenTime")
    public Object findRegisterBetweenTime(
            HttpServletRequest request,
            @RequestParam(value = "dateAfter", defaultValue = "") String dateAfterStr,
            @RequestParam(value = "dateBefore", defaultValue = "") String dateBeforeStr,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "8") int size
    ) {
//        日期传入格式为yyyy-MM-dd HH:mm:ss
//        日期范围为 dateAfter <= date <= dateBefore

        JSONObject object = new JSONObject();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateAfter, dateBefore;
        try {
//            转换为日期
            dateAfter = dateAfterStr.equals("") ? new Date(0) : sdf.parse(dateAfterStr);
            dateBefore = dateBeforeStr.equals("") ? new Date() : sdf.parse(dateBeforeStr);

        } catch (ParseException e) {
            object.put("code", 500);
            object.put("msg", "错误：日期格式不正确");
            return object;
        }

        IPage<RegisterEntity> page = new Page<>(current, size);
        QueryWrapper<RegisterEntity> wrapper = new QueryWrapper<>();
        wrapper.between("time", dateAfter, dateBefore);
        page = registerService.page(page, wrapper);

        if (page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        } else {
            object.put("code", 200);
            object.put("page", page);
        }

        return object;
    }


}
